module ProxyPump
  module Const

    DEFAULT_SERVER_CONF = {
      daemonize: false,
      host: 'localhost',
      port: 9292,
      pidfile: 'tmp/pids/server.pid'
    }

    DEFAULT_PUMP_CONF = {
      daemonize: false,
      pidfile: 'tmp/pids/pump.pid',
      sources: 'sources.yml',
      user_agents: 'user_agents.yml',
      crawl_interval: 600,
      verify_interval: 900,
      cache_interval: 1200,
      delete_interval: 1800,
      min_threads: 3,
      max_threads: 10
    }

    DEFAULT_DATABASE_CONF = {
      file: 'proxy_pump.db',
      pool: 5,
      timeout: 30
    }

    DEFAULT_REDIS_CONF = { url: 'redis://localhost:6379' }

    DEFAULT_LOG_LEVEL = Logger::Severity::DEBUG

    DEFAULT_PROXY_TIMEOUT = { global: false, connect: 2, read: 2 }

    DEFAULT_RETRY_TIMES = 1.freeze
    IPINFO_API_URL      = 'http://ip.taobao.com/service/getIpInfo.php'.freeze
    PING_URL            = 'https://www.baidu.com'.freeze
    IPINFO_MIME         = 'application/json'.freeze

    PROXIES_TABLE_COLUMNS = %i(host port protocal anon country province city area isp source_name source_url last_verified_at score elapsed created_at updated_at).freeze

    DELETE_PROXIES_SQL = 'DELETE FROM proxies WHERE last_verified_at <= ?'.freeze

    DROP_PROXIES_TABLE_SQL = 'DROP TABLE IF EXISTS proxies'.freeze

    CREATE_PROXIES_TABLE_SQL = <<-SQL.freeze
    CREATE TABLE IF NOT EXISTS proxies (
      host             VARCHAR(30)   NOT NULL,
      port             INTEGER       NOT NULL,
      protocal         VARCHAR(20),
      anon             VARCHAR(20),
      country          VARCHAR(50),
      province         VARCHAR(50),
      city             VARCHAR(50),
      area             VARCHAR(50),
      isp              VARCHAR(30),
      source_name      VARCHAR(50),
      source_url       VARCHAR(100),
      last_verified_at DATETIME DEFAULT NULL,
      score            TINYINT DEFAULT 0,
      elapsed          INTEGER,
      created_at       DATETIME NOT NULL,
      updated_at       DATETIME NOT NULL,
      CONSTRAINT pk_proxies_on_host_and_port PRIMARY KEY (host, port)
    );
    CREATE UNIQUE INDEX IF NOT EXISTS index_proxies_on_host_and_port ON proxies(host, port);
    CREATE INDEX IF NOT EXISTS index_proxies_on_anon ON proxies(anon);
    CREATE INDEX IF NOT EXISTS index_proxies_on_score ON proxies(score);
    CREATE INDEX IF NOT EXISTS index_proxies_on_source_name ON proxies(source_name);
    CREATE INDEX IF NOT EXISTS index_proxies_on_source_url ON proxies(source_url);
    SQL

    EMPTY_PROXIES_SQL = 'DELETE FROM proxies'.freeze

    DELETE_PROXY_SQL = 'DELETE FROM proxies WHERE host = ? AND port = ?'.freeze

    EXISTS_PROXY_SQL = 'SELECT COUNT(*) FROM proxies WHERE host = ? AND port = ?'.freeze

    FIND_PROXY_SQL = <<-SQL.strip_heredoc.freeze
    SELECT
      host,
      port,
      protocal,
      anon,
      country,
      province,
      city,
      area,
      isp,
      source_name,
      source_url,
      last_verified_at,
      score,
      elapsed,
      created_at,
      updated_at
    FROM proxies
    WHERE host = ? AND port = ?
    LIMIT 1
    SQL

    UPDATE_PROXY_SQL = <<-SQL.strip_heredoc.freeze
    UPDATE proxies
    SET country = ?, province = ?, city = ?, area = ?, isp = ?, last_verified_at = ?, elapsed = ?, score = ?, updated_at = ?
    WHERE host = ? AND port = ?
    SQL

    INSERT_PROXY_SQL = <<-SQL.freeze
    INSERT INTO proxies(host, port, anon, protocal, country, province, city, area, isp, last_verified_at, elapsed, score, source_name, source_url, created_at, updated_at)
    VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    SQL

    CACHE_PROXIES_SQL = <<-SQL.strip_heredoc.freeze
    SELECT
      host,
      port,
      protocal,
      anon,
      country,
      province,
      city,
      area,
      isp,
      source_name,
      source_url,
      last_verified_at,
      score,
      elapsed,
      created_at,
      updated_at
    FROM proxies
    WHERE last_verified_at >= ?
    ORDER BY elapsed ASC, last_verified_at DESC
    SQL

    VERIFY_PROXIES_SQL = <<-SQL.strip_heredoc.freeze
    SELECT
      host,
      port,
      protocal,
      anon,
      country,
      province,
      city,
      area,
      isp,
      source_name,
      source_url,
      last_verified_at,
      score,
      elapsed,
      created_at,
      updated_at
    FROM proxies
    WHERE last_verified_at <= ?
    SQL

  end # Const
end # ProxyPump
